using System;
using System.Runtime.CompilerServices;

namespace Catastrophe.AutoSulution.TimeCal
{
	public class TimeHelper
	{
		public static readonly string FullFormat = "yyyy/MM/dd HH:mm:ss";

		public static readonly string YmFormat = "yyyy/MM";

		public static readonly string YmdFormat = "yyyy/MM/dd";

		public static readonly string YmdhFormat = "yyyy/MM/dd HH";

		public static readonly string YmdhmFormat = "yyyy/MM/dd HH:mm";

		private const string TimeFlag = ":";

		/// <summary>
		///     计算时间差格式为dd:hh:mm:ss
		/// </summary>
		/// <param name="startT">开始时间</param>
		/// <param name="endT">结束时间</param>
		/// <param name="eTimeExact">计算时间类型</param>
		/// <returns></returns>
		public static double TimeDecFormat(DateTime startT, DateTime endT, ETimeExact eTimeExact = ETimeExact.Seconds)
		{
			TimeSpan ts = new TimeSpan(startT.Ticks);
			TimeSpan timeSpan = new TimeSpan(endT.Ticks).Subtract(ts);
			double num = 0.0;
			switch (eTimeExact)
			{
			case ETimeExact.Seconds:
				num = timeSpan.TotalSeconds;
				break;
			case ETimeExact.Milliseconds:
				num = timeSpan.TotalMilliseconds;
				break;
			}
			if (num < 0.0)
			{
				num = 0.0;
			}
			return num;
		}

		/// <summary>
		///     把总秒数格式化00:00:00:00
		/// </summary>
		/// <param name="sumTime">总时间</param>
		/// <returns></returns>
		public static string TimeToStrFormat(double? sumTime)
		{
			string result = "0:0:0:0";
			if (!sumTime.HasValue)
			{
				return result;
			}
			if (sumTime <= 0.0)
			{
				return result;
			}
			int value = (int)(sumTime / 86400.0).Value;
			double? num = sumTime % 86400.0;
			int value2 = (int)(num / 3600.0).Value;
			num %= 3600.0;
			int value3 = (int)(num / 60.0).Value;
			num %= 60.0;
			DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(0, 7);
			defaultInterpolatedStringHandler.AppendFormatted(value);
			defaultInterpolatedStringHandler.AppendFormatted(":");
			defaultInterpolatedStringHandler.AppendFormatted(value2);
			defaultInterpolatedStringHandler.AppendFormatted(":");
			defaultInterpolatedStringHandler.AppendFormatted(value3);
			defaultInterpolatedStringHandler.AppendFormatted(":");
			defaultInterpolatedStringHandler.AppendFormatted(num, "0");
			return defaultInterpolatedStringHandler.ToStringAndClear();
		}

		/// <summary>
		/// 总时间格式化
		/// </summary>
		/// <param name="startT">开始时间</param>
		/// <param name="endT">结束时间</param>
		/// <param name="eTimeExact">计算时间类型</param>
		/// <returns></returns>
		public static string TimeToStrFormat(DateTime? startT, DateTime? endT, ETimeExact eTimeExact = ETimeExact.Seconds)
		{
			double sumTime = TimeHelper.TimeDecSum(startT, endT, eTimeExact);
			return TimeHelper.TimeToStrFormat(sumTime, eTimeExact);
		}

		/// <summary>
		/// 总时间格式化
		/// </summary>
		/// <param name="sumTime">总时间</param>
		/// <param name="eTimeExact">计算时间类型</param>
		/// <returns></returns>
		public static string TimeToStrFormat(double sumTime, ETimeExact eTimeExact)
		{
			switch (eTimeExact)
			{
			case ETimeExact.Milliseconds:
				sumTime /= 1000.0;
				break;
			case ETimeExact.Minutes:
				sumTime *= 60.0;
				break;
			case ETimeExact.Hours:
				sumTime *= 3600.0;
				break;
			case ETimeExact.Days:
				sumTime *= 86400.0;
				break;
			}
			return TimeHelper.TimeToStrFormat(sumTime);
		}

		/// <summary>
		/// 时间转换
		/// </summary>
		/// <param name="sumTime">总时间</param>
		/// <param name="srcType">源时间类型</param>
		/// <param name="descType">目标时间类型</param>
		/// <param name="digits">位数</param>
		/// <returns></returns>
		public static double TimeConvert(double sumTime, ETimeExact srcType, ETimeExact descType, int digits = 2)
		{
			return srcType switch
			{
				ETimeExact.Seconds => descType switch
				{
					ETimeExact.Days => Math.Round(sumTime / 86400.0, digits), 
					ETimeExact.Hours => Math.Round(sumTime / 3600.0, digits), 
					ETimeExact.Minutes => Math.Round(sumTime / 60.0, digits), 
					ETimeExact.Milliseconds => Math.Round(sumTime * 1000.0, digits), 
					_ => sumTime, 
				}, 
				ETimeExact.Minutes => descType switch
				{
					ETimeExact.Days => Math.Round(sumTime / 1440.0, digits), 
					ETimeExact.Hours => Math.Round(sumTime / 60.0, digits), 
					ETimeExact.Seconds => Math.Round(sumTime * 60.0, digits), 
					ETimeExact.Milliseconds => Math.Round(sumTime * 60000.0, digits), 
					_ => sumTime, 
				}, 
				ETimeExact.Milliseconds => descType switch
				{
					ETimeExact.Days => Math.Round(sumTime / 86400000.0, digits), 
					ETimeExact.Hours => Math.Round(sumTime / 3600000.0, digits), 
					ETimeExact.Minutes => Math.Round(sumTime / 60000.0, digits), 
					ETimeExact.Seconds => Math.Round(sumTime / 1000.0, digits), 
					_ => sumTime, 
				}, 
				ETimeExact.Hours => descType switch
				{
					ETimeExact.Days => Math.Round(sumTime / 24.0, digits), 
					ETimeExact.Minutes => Math.Round(sumTime * 60.0, digits), 
					ETimeExact.Seconds => Math.Round(sumTime * 3600.0, digits), 
					ETimeExact.Milliseconds => Math.Round(sumTime * 3600000.0, digits), 
					_ => sumTime, 
				}, 
				ETimeExact.Days => descType switch
				{
					ETimeExact.Hours => Math.Round(sumTime * 24.0, digits), 
					ETimeExact.Minutes => Math.Round(sumTime * 1440.0, digits), 
					ETimeExact.Seconds => Math.Round(sumTime * 86400.0, digits), 
					ETimeExact.Milliseconds => Math.Round(sumTime * 86400000.0, digits), 
					_ => sumTime, 
				}, 
				_ => 0.0, 
			};
		}

		/// <summary>
		///     格式化并计算总时间
		/// </summary>
		/// <param name="startT">开始时间</param>
		/// <param name="endT">结束时间</param>
		/// <param name="sumTime">总时间</param>
		/// <returns></returns>
		public static string TimeDecFormat(DateTime startT, DateTime endT, ref double sumTime)
		{
			string result = "0:0:0:0";
			TimeSpan ts = new TimeSpan(startT.Ticks);
			TimeSpan timeSpan = new TimeSpan(endT.Ticks).Subtract(ts);
			sumTime = timeSpan.TotalSeconds;
			if (sumTime < 0.0)
			{
				sumTime = 0.0;
			}
			else
			{
				result = timeSpan.TotalDays.ToString("0") + ":" + timeSpan.TotalHours.ToString("0") + ":" + timeSpan.TotalMinutes.ToString("0") + ":" + timeSpan.TotalSeconds.ToString("0");
			}
			return result;
		}

		/// <summary>
		///     距离开始时间n秒后与现在的时间差（&gt;0）
		/// </summary>
		/// <param name="startT">开始时间</param>
		/// <param name="endT">结束时间</param>
		/// <param name="limitSeconds">开始时间以后相加时间</param>
		/// <returns>总时间格式化字符串</returns>
		public static string TimeDecFormat(DateTime startT, DateTime endT, double limitSeconds)
		{
			TimeSpan ts = new TimeSpan(startT.AddSeconds(limitSeconds).Ticks);
			TimeSpan timeSpan = new TimeSpan(endT.Ticks).Subtract(ts);
			string result = "0:0:0:0";
			if (timeSpan.TotalSeconds >= 0.0)
			{
				DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(0, 7);
				defaultInterpolatedStringHandler.AppendFormatted(timeSpan.TotalDays, "0");
				defaultInterpolatedStringHandler.AppendFormatted(":");
				defaultInterpolatedStringHandler.AppendFormatted(timeSpan.TotalHours, "0");
				defaultInterpolatedStringHandler.AppendFormatted(":");
				defaultInterpolatedStringHandler.AppendFormatted(timeSpan.TotalMinutes, "0");
				defaultInterpolatedStringHandler.AppendFormatted(":");
				defaultInterpolatedStringHandler.AppendFormatted(timeSpan.TotalSeconds, "0");
				result = defaultInterpolatedStringHandler.ToStringAndClear();
			}
			return result;
		}

		/// <summary>
		///     n秒后时间
		/// </summary>
		/// <param name="startT">开始时间</param>
		/// <param name="seconds">添加时间</param>
		/// <returns></returns>
		public static DateTime TimeAdd(DateTime startT, double seconds)
		{
			return startT.AddSeconds(seconds);
		}

		/// <summary>
		/// 距离开始时间n秒后与现在的时间差（&gt;0）
		/// </summary>
		/// <param name="startT">开始时间</param>
		/// <param name="endT">结束时间</param>
		/// <param name="limitSeconds">开始时间以后相加时间</param>
		/// <returns></returns>
		public static double TimeDecSum(DateTime? startT, DateTime? endT, double limitSeconds)
		{
			if (!startT.HasValue || !endT.HasValue)
			{
				return 0.0;
			}
			TimeSpan ts = new TimeSpan(startT.Value.AddSeconds(limitSeconds).Ticks);
			TimeSpan timeSpan = new TimeSpan(endT.Value.Ticks);
			TimeSpan timeSpan2 = timeSpan.Subtract(ts);
			return (timeSpan2.TotalSeconds < 0.0) ? 0.0 : timeSpan2.TotalSeconds;
		}

		/// <summary>
		///     计算时间差
		/// </summary>
		/// <param name="startT">开始时间</param>
		/// <param name="endT">结束时间</param>
		/// <param name="eTimeExact">计算时间类型</param>
		/// <returns>天或者小时或者分钟或者秒</returns>
		public static double TimeDecSum(DateTime? startT, DateTime? endT, ETimeExact eTimeExact = ETimeExact.Seconds)
		{
			if (!startT.HasValue || !endT.HasValue)
			{
				return 0.0;
			}
			TimeSpan timeSpan = new TimeSpan(startT.Value.Ticks);
			TimeSpan ts = new TimeSpan(endT.Value.Ticks);
			TimeSpan timeSpan2 = timeSpan.Subtract(ts).Duration();
			double result = 0.0;
			switch (eTimeExact)
			{
			case ETimeExact.Days:
				result = timeSpan2.TotalDays;
				break;
			case ETimeExact.Hours:
				result = timeSpan2.TotalHours;
				break;
			case ETimeExact.Seconds:
				result = timeSpan2.TotalSeconds;
				break;
			case ETimeExact.Minutes:
				result = timeSpan2.TotalMinutes;
				break;
			case ETimeExact.Milliseconds:
				result = timeSpan2.TotalMilliseconds;
				break;
			}
			return result;
		}

		/// <summary>
		/// 过期检测
		/// </summary>
		/// <param name="startT">开始时间</param>
		/// <param name="seconds">经历时间</param>
		/// <returns></returns>
		public static bool IsExpire(DateTime startT, int seconds)
		{
			return DateTime.Compare(DateTime.Now, startT.AddSeconds(seconds)) >= 0;
		}
	}
}
